//
//  SetViewController.m
//  LazyCat
//
//  Created by zhanshu on 15/9/14.
//  Copyright (c) 2015年 zhanshu. All rights reserved.
//

#import "SetViewController.h"
#import "MyTableViewCell.h"
@interface SetViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    NSArray *imgArr;
    NSArray *tileArr;
    NSString *updateURL;
}
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end

@implementation SetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self initNav:@"设置" doBack:YES];
    imgArr = @[@"2323_r2_c2.png",@"设置_r1_c1.png"];
    tileArr = @[@"关于我们",@"版本更新"];
    
    _tableView.tableFooterView = [[UIView alloc] init];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return tileArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *Cell = @"cell";
    MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Cell];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"MyTableViewCell" owner:self options:nil] firstObject];
    }
    if (indexPath.row == 0) {
        cell.detailLable.hidden = YES;
    }else {
        cell.detailLable.hidden = NO;
    }
    NSString *currentVS =  [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
    cell.detailLable.text = currentVS;
    cell.titleLable.text = tileArr[indexPath.row];
    cell.imgView.image = [UIImage imageNamed:imgArr[indexPath.row]];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == 0) {
        //关于我们
        Html5ViewController *html5 = [[Html5ViewController alloc] init];
        html5.doDetailType = NO;
        html5.titleStr = @"关于我们";
        html5.requestUrl = [NSString stringWithFormat:AboutUs,[AppData share].username,[Utils md5SignStr]];
        [self.navigationController pushViewController:html5 animated:YES];
    }else if (indexPath.row == 1) {
        [self getVersionMess];
    }
}

#pragma mark - 版本信息
- (void)getVersionMess {
    NSString *url = [NSString stringWithFormat:UpdateVersion,[Utils md5SignStr]];
    [NTNetUtil sendRequestWithUrl:url requestName:UpdateVersion method:NTRequestMethodGET parameters:nil block:^(NTHttpRequest *networkRequest, NSError *error) {
        NSDictionary *dic = networkRequest.responseData;
        NSString *suc = dic[@"r"];
        if ([suc isEqualToString:@"T"]) {
            NSDictionary *ios = dic[@"iOS"];
//            NSString *currentVS =  [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
            if (![ios[@"isnew"] isEqualToString:@"false"]) {
                NSString *need = [ios[@"need"] safeString];
                NSString *note = [ios[@"note"] safeString];
                NSString *title = [ios[@"title"] safeString];
                if ([need isEqualToString:@"false"]) {
                    //不强制
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:note delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"更新", nil];
                    alert.tag = 100;
                    [alert show];
                }else {
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:note delegate:self cancelButtonTitle:@"更新" otherButtonTitles:nil, nil];
                    alert.tag = 101;
                    [alert show];
                }
                updateURL = [ios[@"url"] safeString];
            }else {
                [[MessShowView share] showWithMess:@"当前为最新版本"];
            }
        }
    }];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    if (alertView.tag == 100) {
        if (buttonIndex == 1) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",updateURL]]];
        }
    }else {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://%@",updateURL]]];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
